﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>工具提示样式的鼠标菜单 - 脚本示例 | AutoHotkey v2</title>
<meta name="description" content="The tooltip mouse menu script displays a popup menu in response to briefly holding down the middle mouse button." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>工具提示样式的鼠标菜单 <span class="headnote">作者: Rajat</span></h1>

<p>此脚本显示弹出菜单来响应短暂地按下鼠标中键的动作. 左键点击来选择菜单项. 在菜单外点击来取消菜单. 最近的改进是可以根据活动的窗口类型来改变菜单的内容(这里使用了记事本和 Word 作为例子).
</p>
<p><a href="TooltipMouseMenu.ahk">下载此脚本</a> &nbsp;| &nbsp;<a href="index.htm">其他示例脚本</a> &nbsp;| &nbsp;<a href="../AutoHotkey.htm">主页</a></p>

<pre class="NoIndent"><em>; You can set any title here for the menu:</em>
MenuTitle := "-=-=-=-=-=-=-=-"

<em>; This is how long the mouse button must be held to cause the menu to appear:</em>
UMDelay := 20

#SingleInstance


<em>;___________________________________________
;_____Menu Definitions______________________</em>

<em>; Create / Edit Menu Items here.
; You can't use spaces in keys/values/section names.</em>

<em>; Don't worry about the order, the menu will be sorted.</em>

MenuItems := "Notepad/Calculator/Section 3/Section 4/Section 5"


<em>;___________________________________________
;______Dynamic menuitems here_______________</em>

<em>; Syntax:
;     Dyn[#] := "MenuItem|Window title"</em>

Dyn := [ "MS Word|- Microsoft Word"
       , "Notepad II|- Notepad" ]

<em>;___________________________________________</em>

Exit


<em>;___________________________________________
;_____Menu Sections_________________________</em>

<em>; Create / Edit Menu Sections here.</em>

Notepad:
Run "Notepad.exe"
return

Calculator:
Run "Calc"
return

Section3:
MsgBox "You selected 3"
return

Section4:
MsgBox "You selected 4"
return

Section5:
MsgBox "You selected 5"
return

MSWord:
MsgBox "this is a dynamic entry (word)"
return

NotepadII:
MsgBox "this is a dynamic entry (notepad)"
return


<em>;___________________________________________
;_____Hotkey Section________________________</em>

~MButton::
HowLong := 0
Loop
{
    HowLong++
    Sleep 10
    if !GetKeyState("MButton", "P")
        Break
}
if HowLong &lt; UMDelay
    return


<em>; Prepares dynamic menu:</em>
DynMenu := ""
For i, item in Dyn
{
    mp := StrSplit(item, "|")
    if WinActive(mp[2])
        DynMenu .= "/" mp[1]
}


<em>; Joins sorted main menu and dynamic menu, and
; clears earlier entries and creates new entries:</em>
MenuItem := StrSplit(Sort(MenuItems, "D/") DynMenu, "/")

<em>; Creates the menu:</em>
Menu := MenuTitle
For i, item in MenuItem
    Menu .= "`n" item

MouseGetPos mX, mY
HotKey "~LButton", "MenuClick"
HotKey "~LButton", "On"
ToolTip Menu, mX, mY
WinActivate MenuTitle
return


MenuClick:
HotKey "~LButton", "Off"
if !WinActive(MenuTitle)
{
    ToolTip
    return
}

MouseGetPos mX, mY
ToolTip
mY -= 3   <em>; Space after which first line starts.</em>
mY /= 13  <em>; Space taken by each line.</em>
if mY &lt; 1
    return
if mY &gt; MenuItem.Length
    return
TargetSection := MenuItem[Round(mY)]
Gosub(StrReplace(TargetSection, "`s"))
return
</pre>
</body>
</html>